package com.sale.system.controller;

import com.sale.common.core.domain.R;
import com.sale.common.core.web.controller.BaseController;
import com.sale.common.log.annotation.Log;
import com.sale.common.log.enums.BusinessType;
import com.sale.common.mybatis.core.page.PageQuery;
import com.sale.common.mybatis.core.page.TableDataInfo;
import com.sale.system.api.domain.dto.FinishAndRawDto;
import com.sale.system.domain.dto.MaterialFinishedDto;
import com.sale.system.domain.vo.MaterialFinishedVo;
import com.sale.system.service.IMaterialFinishedService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description 成品信息
 * @Param
 * @Date 23-07-12
 * @Author hl
 **/
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/finished")
public class MaterialFinishedController extends BaseController {

    private final IMaterialFinishedService materialFinishedService;

    /**
     * 获取成品配置
     */
    //@SaCheckPermission("system:finished:list")
    @GetMapping("/list")
    public TableDataInfo<MaterialFinishedDto> list(MaterialFinishedVo materialFinished, PageQuery pageQuery) {
        return materialFinishedService.selectFinishedList(materialFinished, pageQuery);
    }

    /**
     * 新增成品信息
     */
    //@SaCheckPermission("system:finished:add")
    @Log(title = "成品信息", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Void> add(@Validated @RequestBody MaterialFinishedVo materialFinished) throws Exception {
        return toAjax(materialFinishedService.insertFinished(materialFinished));
    }

    /**
     * 删除成品信息
     */
    //@SaCheckPermission("system:finished:remove")
    @Log(title = "成品信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{materialId}")
    public R<Void> remove(@PathVariable("materialId") String materialId) throws Exception {
        return toAjax(materialFinishedService.deleteFinishedById(materialId));
    }

    /**
     * 根据编号获取详细信息
     */
    @GetMapping(value = "/{materialId}")
    //@SaCheckPermission("system:finished:query")
    public R<Map<String, Object>> getInfo(@PathVariable long materialId) {
        Map<String, Object> ajax = new HashMap<>();
        ajax.put("detailed", materialFinishedService.selectFinishedById(materialId));
        return R.ok(ajax);
    }

    /**
     * 修改成品信息
     */
    //@SaCheckPermission("system:finished:edit")
    @Log(title = "成品信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@RequestBody MaterialFinishedVo materialFinished) throws Exception {
        return toAjax(materialFinishedService.updateMaterialFinishedEdit(materialFinished));
    }

    /**
     * 树结构
     *
     * @param
     * @return 成品集合
     */
    @GetMapping("/listChildren")
    public R<Map<String, Object>> listChildren() {
        Map<String, Object> ajax = new HashMap<>();
        ajax.put("theDrop", materialFinishedService.selectChildrenFinishedThe());
        return R.ok(ajax);
    }

    /**
     * 原料冻结
     */
    //@RequiresPermissions("material:awmateria:editFreeze")
    @Log(title = "成品信息", businessType = BusinessType.UPDATE)
    @PutMapping("/editFreeze/{materialId}")
    public R editFreeze(@PathVariable Long materialId) {
        return R.ok(materialFinishedService.updateEditFreeze(materialId));
    }

    /**
     * 原料解冻
     */
    // @RequiresPermissions("material:awmateria:editThaw")
    @PutMapping("/editThaw/{materialId}")
    public R editThaw(@PathVariable Long materialId) {
        return R.ok(materialFinishedService.updateEditThaw(materialId));
    }

    /**
     * 根据物料编码查询详情(成品信息)
     */
    @GetMapping("/selectCode")
    public R<Map<String, Object>> selectCode(String materialCode) {
        Map<String, Object> ajax = new HashMap<>();
        ajax.put("materialFinished", materialFinishedService.selectMaterialFinishedMaterialCode(materialCode));
        return R.ok(ajax);
    }


    /**
     * 根据物料编码查询详情（成品信息）
     *
     * @param materialCode
     * @return
     */
    @GetMapping("/getByFinishedMaterialCode/{materialCode}")
    public MaterialFinishedDto getByFinishedMaterialCode(@PathVariable String materialCode) {
        return materialFinishedService.getByFinishedMaterialCode(materialCode);
    }

    /**
     * 根据编码获取物料信息（财务成品调整模块使用）
     */
    @GetMapping("/getFinishAndRaw")
    public R<FinishAndRawDto> getFinishAndRaw(String materialCode) {
        return R.ok(materialFinishedService.getFinishAndRaw(materialCode));
    }

}
